<template>
    <div class="users">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>活动管理</el-breadcrumb-item>
        <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      </el-breadcrumb>
      <el-card>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input
              placeholder="请输入内容"
              v-model="queryinfo.query"
              clearable
              @clear="gettableData"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="gettableData"
              ></el-button>
            </el-input>
          </el-col>
          <el-col :span="6">
            <el-button @click="addFormVisible = true">添加用户</el-button>
          </el-col>
        </el-row>
        <!-- 表格 -->
        <el-table :data="tableData" border>
          <el-table-column type="index" label="#"> </el-table-column>
          <el-table-column prop="username" label="用户名"> </el-table-column>
          <el-table-column prop="email" label="邮箱"> </el-table-column>
          <el-table-column prop="mobile" label="电话"> </el-table-column>
          <el-table-column prop="role_name" label="角色"> </el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope">
              <el-switch @change="tai(scope.row)" v-model="scope.row.mg_state"  active-color="pink" inactive-color="red">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="scope">
              <el-button @click="del(scope.row.id)" type="text" size="small"
                >删除</el-button
              >
              <el-button @click="send(scope.row.id)" type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="xg(scope.row.id)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryinfo.pagenum"
          :page-sizes="[1, 5, 10, 15]"
          :page-size="queryinfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-card>
      <el-dialog title="添加用户" :visible.sync="addFormVisible">
        <el-form :model="addForm" ref="addRef" :rules="addRules">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="addForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="addForm.password"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="addForm.email"></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="mobile">
            <el-input v-model="addForm.mobile"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addFormVisible = false">取 消</el-button>
        
          <el-button type="primary" @click="add()">确 定</el-button>
        </div>
      </el-dialog>
       <!-- 修改的对话框 -->
       <el-dialog title="编辑用户" :visible.sync="dialogFormVisible">
        <el-form :model="editForm" ref="editRef" :rules="editRules">
          <el-form-item label="用户名">
            <el-input
              v-model="editForm.username"
              autocomplete="off"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="editForm.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="editForm.mobile" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="edit()">修改</el-button>
        </div>
      </el-dialog>
      <!-- 分配权限 -->
      <el-dialog title="编辑用户" :visible.sync="sendFormVisible">
        <el-form :model="editObj" >
          <el-form-item label="请选择角色">
            
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="sendFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="send()">确定</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  export default {
    data() {
      return {
        tableData: [],
        addForm:{},
        editForm:{},
        addFormVisible:false,
        dialogFormVisible:false,
        sendFormVisible:false,
        queryinfo: {
          query: "",
          pagenum: 1,
          pagesize: 10,
        },
        total: 0,
        addRules:{
           username:[
              {required:true,message:'请输入用户名',trigger:'blur'}
           ],
           password:[
              {required:true,message:'请输入密码',trigger:'blur'}
           ],
           email:[
              {required:true,message:'请输入邮箱',trigger:'blur'}
           ],
           mobile:[
              {required:true,message:'请输入手机号',trigger:'blur'}
           ],
        },
        editRules:{
           username:[
              {required:true,message:'请输入用户名',trigger:'blur'}
           ],
           email:[
              {required:true,message:'请输入邮箱',trigger:'blur'}
           ],
           mobile:[
              {required:true,message:'请输入手机号',trigger:'blur'}
           ],
        }
      };
    },
    created() {
      this.gettableData();
    },
    methods: {
      async gettableData() {
        const { data } = await this.axios.get("/users/", {
          params: this.queryinfo,
        });
  
     
  
        this.tableData = data.data.users;
  
        this.total = data.data.total;
      },
      handleSizeChange(val) {
        this.queryinfo.pagesize = val;
        this.gettableData();
      },
      handleCurrentChange(val) {
        this.queryinfo.pagenum = val;
        this.gettableData();
      },
      del(id) {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            const { data } = await this.axios.delete("/users/" + id);
            if (data.meta.status === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.gettableData();
            }
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      },
      async tai(row) {
        const { data } = await this.axios.put(
          `/users/${row.id}/state/${row.mg_state}`
        );
  
        this.gettableData();
  
        this.$message.success(data.meta.msg);
      },
      add(){
         this.$refs.addRef.validate(async valid=>{
             if(valid){
                 const {data}=await this.axios.post('/users/',this.addForm)
                 if(data.meta.status===201){
                    this.gettableData()
                    this.addFormVisible=false
                    this.$message.success(data.meta.msg)
  
                    this.addForm={
                        username:'',
                        password:'',
                        mobile:'',
                        email:'',
                    }
                 }else{
                  this.$message.error(data.meta.msg)
                 }
             }
         })
      },
       // 回显
       async xg(id) {
     
        this.dialogFormVisible = true;
     
        const { data } = await this.axios.get("/users/" + id);
       
        this.editForm = data.data;
      
        this.gettableData();
      },
      //确认修改
      edit() {
        this.$refs.editRef.validate(async (valid) => {
      
          if (valid) {
         
            const { data } = await this.axios.put("/users/" + this.editForm.id,this.editForm
            );
          
            console.log(data);
          
            if (data.meta.status === 200) {
          
              this.$message.success(data.meta.msg);
            
              this.dialogFormVisible = false;
         
              this.gettableData();
            } else {
        
              this.$message.error(data.meta.msg);
            }
          }
        });
      },
      send(id){
        this.sendFormVisible=true
      },
    },
  };
  </script>